const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    pageNum: 1,
    pageSize: 10,
    isRequest: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: options.type === '1' ? '已学课程' : '课程收藏'
    })
    //this.getFavoritesCourse();
  },
  onShow() {
    this.setData({
      pageNum: 1,
      list: []
    })
    this.getFavoritesCourse();
  },
  getFavoritesCourse() {
    if (!this.data.isRequest) return;
    app.fetchRequest({
      url: `/user/exam/query/${this.options.type}`,
      data: {
        pageNum: this.data.pageNum,
        pageSize: this.data.pageSize
      },
      success: (res) => {
        wx.stopPullDownRefresh();
        if (res.list.length) {
          if (this.data.list.length) {
            this.data.list.push(...res.list)
            this.setData({
              list: this.data.list,
              pageNum: ++this.data.pageNum
            })
          } else {
            this.setData({
              list: res.list,
              pageNum: ++this.data.pageNum
            })
          }
        } else {
          this.setData({
            isRequest: false
          })
        }
      }
    })
  },
  toCourse(e) {
    const courseId = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: `../course/course?courseId=${courseId}`,
    })
  },
  onReachBottom() {
    this.getFavoritesCourse();
  },
  onPullDownRefresh() {
    this.setData({
      pageNum: 1,
      list: [],
      isRequest: true
    });
    this.getFavoritesCourse();
  }
})